<template>
  <div class="order" >
    <commhead-2 name="订单页"/>
    <div class="container">
      <van-contact-card
        :type="typeState"
        :name="addres.name"
        :tel="addres.tel"
        @click="choose" />
        <van-card
          :num="item.number"
          :price="item.price"
          :title="item.name"
          :thumb="item.pic"
          v-for = "item in list"
          :key="item.id"
        />
    </div>
    <van-submit-bar :price="price*100" button-text="提交订单" @submit="onSubmit" />
  </div>
</template>

<script>
import { fetchAddress, fetchCartInfo, getContactInfo, createOrder } from '_api'
import Commhead2 from '../../components/Commhead2.vue'
import { getToken } from '_utils'
import { Toast } from 'vant'
export default {
  data () {
    return {
      addres: {
        name: '',
        tel: ''
      },
      price: 0,
      list: [],
      typeState: 'add'
    }
  },
  mounted () {
    this.getContactInfo()
    this.fetchCartInfo()
    this.fetchAddress()
  },
  methods: {
    // 提交
    onSubmit () {
      createOrder({
        goodsJsonStr: JSON.stringify(this.list),
        address: this.address,
        token: getToken()
      }).then(res => {
        if (res.data.code === 0) {
          Toast({
            type: 'success',
            duration: 1000,
            message: '提交成功',
            onClose: () => {
              this.$router.go(-1)
            }
          })
        }
      })
    },
    // 获取收货人详情
    getContactInfo () {
      if (JSON.stringify(this.$route.query) !== '{}') {
        getContactInfo({
          token: getToken(),
          id: this.$route.query.id
        }).then(res => {
          this.typeState = 'edit'
          this.addres.name = res.data.data.info.linkMan
          this.addres.tel = res.data.data.info.mobile
        })
      }
    },
    // 选择收货人
    choose () {
      this.$router.replace({
        path: '/addresslist',
        query: {
          from: '/order'
        }
      })
    },
    // 获取购物车选中
    fetchCartInfo () {
      fetchCartInfo({
        token: getToken()
      }).then(res => {
        this.list = res.data.data.items.filter(item => item.selected)
        console.log(res.data.data.price)
        this.price = res.data.data.price
      })
    },
    // 获取所有的地址
    fetchAddress () {
      if (JSON.stringify(this.$route.query) === '{}') {
        fetchAddress({
          token: getToken()
        }).then(res => {
          if (res.data.code === 0) {
            this.typeState = 'edit'
            const ad = res.data.data.result.filter(item => item.isDefault)
            if (ad.length > 0) {
            // 这是有默认值
              this.addres.name = ad[0].linkMan
              this.addres.tel = ad[0].mobile
            } else {
              this.addres.name = res.data.data.result[0].linkMan
              this.addres.tel = res.data.data.result[0].mobile
            }
          } else {
            this.typeState = 'add'
          }
        })
      }
    },
    onAdd () {
    }
  },
  components: { Commhead2 }

}
</script>

<style lang="scss" scoped>
.order{
  font-size: 16px;
  text-align: left;
}
.container{
  padding-top: 40px;
}
</style>
